import 'package:example/app/data/local/local_login_model_repository.dart';
import 'package:example/app/data/repository/wan_android_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_framework/flutter_base_framework.dart';

class LoginController extends BaseViewModel {
  late TextEditingController userNameController;
  late TextEditingController passwordController;

  @override
  void onInit() {
    userNameController = TextEditingController();
    passwordController = TextEditingController();
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  login() async {
    var account = userNameController.text.toString().trim();
    var password = passwordController.text.toString().trim();

    if (account.isEmpty) {
      showToast('请输入用户名');
      return;
    }

    if (password.isEmpty) {
      showToast('请输入密码');
      return;
    }

    try{
      Get.loading();
      var loginBean = await WanAndroidRepository.login(account, password);
      showToast('登录成功');
      LocalLoginModelRepository.saveLoginModel(loginBean);
    }catch(e,s){
      var error = handlerError(e, s);
      showToast(error.message!);
    }finally{
      Get.dismiss();
    }
  }
}
